package com.iafme.config;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;

import cn.hutool.core.util.StrUtil;
import com.systemframework.redis.RedisCache;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author summer
 * @date 2023-11-19 11:27
 */
@Configuration
@Data
@AllArgsConstructor
public class SeatNumGenerate {

    private final RedisCache redisUtils;
    /**
     * 生成订单唯一编码 202311190001
     * @return
     */
    public  String seatNum (Integer showingsId,String city){
        Object object = redisUtils.get("order_no:"+showingsId+"_"+city);
        String maxNo="";
        if(ObjectUtil.isNotNull(object)){
            maxNo= Convert.convert(String.class,object);
        }
        if (StrUtil.isNotEmpty(maxNo)) {
            int endNum = Integer.parseInt(maxNo);
            int tmpNum = 10000 + endNum + 1;
             maxNo =  subStr("" + tmpNum, 1);
        }else{
            maxNo="0001";
        }
        redisUtils.set("order_no:"+showingsId+"_"+city ,maxNo,7, TimeUnit.DAYS);
        return maxNo;
    }





    public   String subStr(String str, int start) {
        if (str == null || "".equals(str) || str.length() == 0) {
            return "";
        } else if (start < str.length()) {
            return str.substring(start);
        } else {
            return "";
        }

    }
}
